package Member;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;

public class Delete extends JPanel {
	Data6[] d;
	int k=0;
	Data6 data = new Data6();
	
	private JLabel label;
	private JTextField nameInput;
	private JLabel label_1;
	private JTextField addressInput;
	private JLabel label_2;
	private JTextField phoneInput;
	private JLabel label_3;
	private JTextField emailInput;
	private JLabel label_4;
	private JTextField ageInput;
	private JButton btnDel;
	private JLabel lblMemberDelete;
	private JLabel status;
	private JButton btnPrev;
	private JButton btnNext;
	private JButton btnFind;
	
	
	
	// null 값을 저장하면 데이터 삭제
	
	public Delete() {
		setSize(new Dimension(450, 500));
		setBackground(new Color(250, 128, 114));
		setLayout(null);
		add(getLabel());
		add(getNameInput());
		add(getLabel_1());
		add(getAddressInput());
		add(getLabel_2());
		add(getPhoneInput());
		add(getLabel_3());
		add(getEmailInput());
		add(getLabel_4());
		add(getAgeInput());
		add(getBtnDel());
		add(getLblMemberDelete());
		add(getStatus());
		add(getBtnPrev());
		add(getBtnNext());
		add(getBtnFind());

	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("Name");
			label.setBounds(120, 79, 65, 23);
		}
		return label;
	}
	private JTextField getNameInput() {
		if (nameInput == null) {
			nameInput = new JTextField();
			nameInput.setColumns(10);
			nameInput.setBounds(197, 79, 52, 23);
		}
		return nameInput;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Address");
			label_1.setBounds(120, 123, 65, 23);
		}
		return label_1;
	}
	private JTextField getAddressInput() {
		if (addressInput == null) {
			addressInput = new JTextField();
			addressInput.setColumns(10);
			addressInput.setBounds(197, 123, 116, 23);
		}
		return addressInput;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("Phone");
			label_2.setBounds(120, 167, 65, 23);
		}
		return label_2;
	}
	private JTextField getPhoneInput() {
		if (phoneInput == null) {
			phoneInput = new JTextField();
			phoneInput.setColumns(10);
			phoneInput.setBounds(197, 167, 116, 23);
		}
		return phoneInput;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("Email");
			label_3.setBounds(120, 211, 65, 23);
		}
		return label_3;
	}
	private JTextField getEmailInput() {
		if (emailInput == null) {
			emailInput = new JTextField();
			emailInput.setColumns(10);
			emailInput.setBounds(197, 211, 116, 23);
		}
		return emailInput;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("Age");
			label_4.setBounds(120, 255, 65, 23);
		}
		return label_4;
	}
	private JTextField getAgeInput() {
		if (ageInput == null) {
			ageInput = new JTextField();
			ageInput.setColumns(10);
			ageInput.setBounds(197, 255, 116, 23);
		}
		return ageInput;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("Del");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					boolean flag = true; // 모든 값을 입력했을때 
					if(nameInput.getText().equals("")) {
						flag=false;
						status.setText("Find First");
					}
					else if(addressInput.getText().equals("")) flag=false;
					else if(phoneInput.getText().equals("")) flag=false;
					else if(emailInput.getText().equals("")) flag=false;
					else if(ageInput.getText().equals("")) flag=false;
					if(flag){
						Data6 data = new Data6();						
						d[k]=null;
						//입력 종료후 모든 칸을 공백으로
						nameInput.setText(""); 
						addressInput.setText("");
						phoneInput.setText("");
						emailInput.setText("");
						ageInput.setText("");
						
						//커서를 NameInput 칸으로 이동
						nameInput.requestFocus();
							
						status.setText("Delete successful");
								
								
							
						
					}
					
				}
			});
			btnDel.setBounds(248, 297, 65, 23);
		}
		return btnDel;
	}
	private JLabel getLblMemberDelete() {
		if (lblMemberDelete == null) {
			lblMemberDelete = new JLabel("Member Delete");
			lblMemberDelete.setFont(new Font("Segoe Print", Font.BOLD, 16));
			lblMemberDelete.setBounds(120, 24, 200, 50);
		}
		return lblMemberDelete;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("                Please Input Your Data");
			status.setOpaque(true);
			status.setBackground(new Color(255, 218, 185));
			status.setBounds(120, 331, 193, 15);
		}
		return status;
	}
	private JButton getBtnPrev() {
		if (btnPrev == null) {
			btnPrev = new JButton("Prev");
			btnPrev.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				}
			});
			btnPrev.setBounds(120, 297, 65, 23);
		}
		return btnPrev;
	}
	private JButton getBtnNext() {
		if (btnNext == null) {
			btnNext = new JButton("Next");
			btnNext.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				}
			});
			btnNext.setBounds(184, 297, 65, 23);
		}
		return btnNext;
	}
	
	public void setD(Data6[] a){
		this.d=a;
	}
	
	private JButton getBtnFind() {
		if (btnFind == null) {
			btnFind = new JButton("Find");
			btnFind.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {					
					for(k=0;k<d.length;k++){
						if(d[k]==null){
							status.setText("Not Exist");
							nameInput.setText("");
							addressInput.setText("");
							phoneInput.setText("");
							emailInput.setText("");
							ageInput.setText("");
							nameInput.requestFocus();							
							break;
						}
						if(nameInput.getText().equals(d[k].name)){
							nameInput.setText(d[k].name); 
							addressInput.setText(d[k].address);
							phoneInput.setText(d[k].number);
							emailInput.setText(d[k].email);
							ageInput.setText(String.valueOf(d[k].age));
							status.setText("Member, " + d[k].name + " is loaded.");
							break;
						}
					}
				}
			});
			btnFind.setBounds(248, 79, 65, 23);
		}
		return btnFind;
	}
}
